package com.cdc.mobileassistant.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.cdc.mobileassistant.ui.bean.FragmentInfo;
import com.cdc.mobileassistant.ui.fragment.CategoryFragment;
import com.cdc.mobileassistant.ui.fragment.GamesFragment;
import com.cdc.mobileassistant.ui.fragment.Homeragment;
import com.cdc.mobileassistant.ui.fragment.RankingFragment;
import com.cdc.mobileassistant.ui.fragment.RecommendFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/3/22.
 */

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private List<FragmentInfo> mFragments=new ArrayList<>(4);
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        initFragments();
    }

    private  void initFragments(){

        mFragments.add(new FragmentInfo("推荐",RecommendFragment.class));
        mFragments.add(new FragmentInfo("排行",RankingFragment.class));
        mFragments.add(new FragmentInfo("游戏",GamesFragment.class));
        mFragments.add(new FragmentInfo("分类",CategoryFragment.class));


    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;
        try {
          fragment=(Fragment) mFragments.get(position).getFragment().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragments.get(position).getTitle();
    }
}
